﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace PluginManager
{
	class Program
	{
		static void Main(string[] args)
		{
			if (args.Length != 3)
			{
				PrintUsage();
				return;
			}

			List<FlexLib.Plugins.PluginManager> managers = new List<FlexLib.Plugins.PluginManager>();
			switch (args[0])
			{
				case "--host":
				case "-h":
					managers.Add(FlexLib.Plugins.PluginManager.HostInstance);
					break;

				case "--admin":
				case "-a":
					managers.Add(FlexLib.Plugins.PluginManager.AdminInstance);
					break;

				case "--both":
				case "-b":
					managers.Add(FlexLib.Plugins.PluginManager.HostInstance);
					managers.Add(FlexLib.Plugins.PluginManager.AdminInstance);
					break;

				default:
					Console.WriteLine("Unknown option " + args[0]);
					PrintUsage();
					return;
			}

			switch (args[1])
			{
				case "--install":
				case "-i":
					foreach (FlexLib.Plugins.PluginManager manager in managers)
					{
						Install(manager, args[2]);
					}
					return;

				case "--uninstall":
				case "-u":
					foreach (FlexLib.Plugins.PluginManager manager in managers)
					{
						Uninstall(manager, args[2]);
					}
					return;

				default:
					Console.WriteLine("Unknown option " + args[1]);
					PrintUsage();
					return;
			}
		}

		static void Install(FlexLib.Plugins.PluginManager manager, string path)
		{
			try
			{
				manager.Install(path);
			}
			catch (Exception ex)
			{
				Console.WriteLine(ex.Message);
				return;
			}

			Console.WriteLine("Successful");
		}

		static void Uninstall(FlexLib.Plugins.PluginManager manager, string name)
		{
			try
			{
				manager.Uninstall(name);
			}
			catch (Exception ex)
			{
				Console.WriteLine(ex.Message);
				return;
			}

			Console.WriteLine("Successful");
		}

		static void PrintUsage()
		{
			Console.WriteLine("Usage:");
			Console.WriteLine("PluginManager side option parameter");
			Console.WriteLine();
			Console.WriteLine("Sides:");
			Console.WriteLine("--host");
			Console.WriteLine(" -h           host side.");
			Console.WriteLine("--admin");
			Console.WriteLine(" -a           admin side.");
			Console.WriteLine("--both");
			Console.WriteLine(" -b           both sides.");
			Console.WriteLine();
			Console.WriteLine("Options:");
			Console.WriteLine("--install");
			Console.WriteLine(" -i           install file, specified by parameter.");
			Console.WriteLine("--uninstall");
			Console.WriteLine(" -u           uninstall plugin, specified by parameter.");
		}
	}
}
